package test.socket;

import hyl.base.net.IData1;
import hyl.base.net.IpgReader;
import hyl.base.net.bio.BIOData1;
import hyl.base.net.bio.BIOSocketSrv;
import hyl.core.MyDate;
import hyl.core.MyFun;
import hyl.core.net.pg.Netpg2Cmd;
import hyl.core.run.IDo1;

public class testbioserver {
	public static void main(String[] arg) throws Exception {

		BIOSocketSrv bioc = new BIOSocketSrv(9090);
		testbioserver tbs = new testbioserver();
		//tbs.test4_2接收数据中断再读(bioc);

		 tbs.test1收发数据3次(bioc);
		// tbs.test2指令处理(bioc);
	  // tbs.test3url服务注册(bioc);
		// tbs.test4读取连续数据(bioc);
	}

	public void test4_2接收数据中断再读(BIOSocketSrv bs) {
		IDo1<IpgReader> handle = new IDo1<IpgReader>() {
			@Override
			public void run(IpgReader br) {
				System.out.println("新连接");
				byte[] data;
				int i = 0;
				long n = MyFun.getMs();
				while ((data = br.pullData()) != null) {
					System.out.println("收到客户端消息:::" + new String(data));
					if (i++ == 30)
						MyDate.sleep(4000);
					br.send(IData1.B类型_数据, ("反馈客户端=" + i).getBytes());

				}
				long m = (MyFun.getMs() - n) / 1000;
				System.out.println("用时=" + m + "秒");
			}
		};
		bs.set_IDo接收处理函数(handle);
		bs.startServer();
	}

	public void test4读取连续数据(BIOSocketSrv bs) {
		IDo1<IpgReader> handle = new IDo1<IpgReader>() {
	

			@Override
			public void run(IpgReader br) {
				System.out.println("新连接");
				long n = MyFun.getMs();
				while (true) {
					byte[] data = br.pullData();
					if (data == null)
						continue;
					String s = new String(data);
					System.out.println("收到客户端消息:::" + s);
					if (s.equals("bye")) {
						break;
					}

				}
				long m = MyFun.getMs() - n;
				System.out.println("用时=" + m + "m秒");
			}
		};
		bs.set_IDo接收处理函数(handle);
		bs.startServer();
	}

//	public void test3url服务注册(BIOSocketSrv bs) {
//		URIServicesHandle handle = new URIServicesHandle();
//		bs.set_IDo接收处理函数(handle);
//		bs.startServer();
//		System.out.println("开启服务..");
//	}

	public void test2指令处理(BIOSocketSrv bs) {
		IDo1<IpgReader> handle = new IDo1<IpgReader>() {
		

			@Override
			public void run(IpgReader br) {
				System.out.println("新连接");
				while (true) {
					byte[] data = br.pullData();
					if (data == null)
						continue;
					Netpg2Cmd cmd = new Netpg2Cmd(data);
					System.out.println("收到客户端消息:::" + cmd.toString());
					br.send(BIOData1.B类型_数据, ("确认客户端参数=" + cmd.getS字符参数()).getBytes());

				}
			}
		};

		bs.set_IDo接收处理函数(handle);
		bs.startServer();
	}

	public void test1收发数据3次(BIOSocketSrv bs) {
		IDo1<IpgReader> handle = new IDo1<IpgReader>() {
			@Override
			public void run(IpgReader br) {
				System.out.println("新连接");
				byte[] data;
				int i = 0;
				while ((data = br.pullData()) != null) {
					System.out.println("收到客户端消息:::" + new String(data));
					if (i++ < 3)
						// br.send(BIOData1.B类型_未知,("反馈客户端=" + i).getBytes());
						br.send(BIOData1.B类型_数据, ("反馈客户端=" + i).getBytes());
					else
						break;
				}
			}
		};
		bs.set_IDo接收处理函数(handle);
		bs.startServer();

	}

}
